package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.modular.crm.entity.GwdcT;
import cn.e4j.modular.crm.entity.GwdfT;
import cn.e4j.modular.crm.service.impl.Gwdc003ServiceImpl;
import cn.e4j.modular.crm.service.impl.GwdfTServiceImpl;
import cn.e4j.modular.erp.controller.ErpLogic;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 订单变更
 * Created by miaoyl on 2019-03-13.
 */
@Controller
@RequestMapping("/crm/gwai770")
public class Gwai770Controller extends BaseController {

    @Autowired
    private GwdfTServiceImpl gwdfTService;

    @Autowired
    private Gwdc003ServiceImpl gwdc003Service;

    @Autowired
    private ErpLogic erpLogic;

    /**
     * 订单变更管理页面
     */
    @RequiresPermissions("crm:gwai770:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwai770.html";
    }


    /**
     * 查询订单变更列表
     */
    @RequestMapping("/getListPage")
    @RequiresPermissions("crm:gwai770:init")
    @ResponseBody
    public ResponseData getListPage(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = this.gwdfTService.getGwdflist(page, inDto);
        for (int i = 0; i < result.size(); i++) {
            String mlegal = result.get(i).get("GWDFMLEGAL").toString();
            String GWDF003 = result.get(i).get("GWDF003").toString();
            Dto coptc = this.gwdfTService.getCOPTCInfo(mlegal, GWDF003);
            if (coptc != null) {
                result.get(i).put("TC003", coptc.getString("TC003"));
                result.get(i).put("TC040", coptc.getString("TC040"));
            }
        }
        return new PageReData(page, result);
    }

    /**
     * 获取新增数据
     */
    @RequestMapping("/getAddData")
    @RequiresPermissions("crm:gwai770:init")
    @ResponseBody
    public ResponseData getAddData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto addDataDto = gwdfTService.getAddData(inDto);
        return new SuccessResponseData(addDataDto);
    }

    /**
     * 查询订单列表
     */
    @RequestMapping("/getCOPTCList")
    @ResponseBody
    public ResponseData getCOPTCList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwdfTService.getCOPTCList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 查询变更版号
     */
    @RequestMapping("/getGwdf002")
    @RequiresPermissions("crm:gwai750:init")
    @ResponseBody
    public ResponseData getGwdf002(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto result = gwdfTService.getGwdf002(inDto);
        return ResponseData.success(result);
    }

    /**
     * 查询订单单身列表
     */
    @RequestMapping("/getCOPTDList")
    @ResponseBody
    public ResponseData getCOPTDList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwdfTService.getCOPTDList(page, inDto);
        for (int i = 0; i < result.size(); i++) {
            inDto.put("GWCSDOCNO", result.get(i).get("TD049"));
            inDto.put("GWCSSEQ", result.get(i).get("TD048"));
            Dto gwcs112Dto = this.gwdfTService.getGwcs112(inDto);
            if (gwcs112Dto != null) {
                result.get(i).put("GWCS112", gwcs112Dto.getString("GWCS112"));
            } else {
                result.get(i).put("GWCS112", null);
            }
        }
        return new PageReData(page, result);
    }

    /**
     * 查询订单单身列表
     */
    @RequestMapping("/getCOPTDData")
    @RequiresPermissions("crm:gwai770:init")
    @ResponseBody
    public ResponseData getCOPTDData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwdfTService.getCOPTDData(inDto);
        for (int i = 0; i < result.size(); i++) {
            inDto.put("GWCSDOCNO", result.get(i).get("TD049"));
            inDto.put("GWCSSEQ", result.get(i).get("TD048"));
            Dto gwcs112Dto = this.gwdfTService.getGwcs112(inDto);
            if (gwcs112Dto != null) {
                result.get(i).put("GWCS112", gwcs112Dto.getString("GWCS112"));
            } else {
                result.get(i).put("GWCS112", null);
            }
        }
        return new SuccessResponseData(result);
    }

    /**
     * 添加数据
     */
    @RequestMapping("/add")
    @RequiresPermissions("crm:gwai770:init")
    @ResponseBody
    public ResponseData addData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwdfT> gwdfTList = inDto.getReqList(GwdfT.class);
        gwdfTService.addData(gwdfTList);
        try {
            gwdfTService.update(gwdfTList);
        } catch (Exception e) {
            gwdfTService.update2(gwdfTList);
        }
        return SUCCESS_TIP;
    }

    /**
     * 获取变更单单身数据
     */
    @RequestMapping("/getGwdfInfo")
    @RequiresPermissions("crm:gwai770:init")
    @ResponseBody
    public ResponseData getGwdfInfo(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> gwdfInfo = gwdfTService.getGwdfInfo(inDto);
        return new SuccessResponseData(gwdfInfo);
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("crm:gwdc005:init")
    @ResponseBody
    public ResponseData edit(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
        List<GwdfT> gwddTList = JSONObject.parseArray(jsonObject.getString("list1"), GwdfT.class);
        gwdfTService.editData(gwddTList, jsonObject.getString("GWDFDOCNO"));
        try {
            gwdfTService.update(gwddTList);
        } catch (Exception e) {
            gwdfTService.update2(gwddTList);
        }
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwai770:init")
    @ResponseBody
    public ResponseData del(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        gwdfTService.delete(inDto);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/delDataBatch")
    @RequiresPermissions("crm:gwdc005:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwdfT> gwdfTList = inDto.getReqList(GwdfT.class);
        gwdfTService.delDataBatch(gwdfTList);
        return SUCCESS_TIP;
    }

    /**
     * 审核
     */
    @RequestMapping("/check")
    @RequiresPermissions("crm:gwai770:init")
    @ResponseBody
    public ResponseData check(@DtoParam DtoWapper dtoWapper) {
        Dto outDto = Dtos.newDto();
        Dto inDto = dtoWapper.getDto();
        try {
            outDto = erpLogic.getERPService("checkGwai770",inDto);
            if(!outDto.getString("code").equals(200)){
                return ResponseData.success(outDto);
            }
        }catch (NoSuchMethodException e) {
            e.printStackTrace();
        }catch(Exception e) {
            e.printStackTrace();
        }
        return SUCCESS_TIP;
        /*List<GwdfT> gwdfTList = this.gwdfTService.getGwdfData(inDto);
        List<Dto> change = new ArrayList();
        int count_1 = 0;
        int count_2 = 0;
        for (GwdfT gwdfT : gwdfTList) {
            Dto queryDto = Dtos.newDto();
            queryDto.put("mlegal", gwdfT.getGWDFMLEGAL());
            inDto.put("mlegal", gwdfT.getGWDFMLEGAL());
            queryDto.put("TH015", gwdfT.getGWDF003());
            queryDto.put("TH016", gwdfT.getGWDF004());
            Dto sumTh008 = gwdfTService.getSumTh008(queryDto);
            if (sumTh008.getBigDecimal("TH008_SUM").compareTo(gwdfT.getGWDF015()) == 0) {
                if (gwdfT.getGWDF015().compareTo(gwdfT.getGWDF014()) < 1) {
                    continue;
                } else {
                    count_2++;
                }
            } else {
                Dto changeDto = Dtos.newDto();
                changeDto.put("GWDF003", gwdfT.getGWDF003());
                changeDto.put("GWDF004", gwdfT.getGWDF004());
                changeDto.put("GWDF015", sumTh008.getBigDecimal("TH008_SUM"));
                change.add(changeDto);
                count_1++;
            }
        }
        if (count_1 > 0) {
            outDto.put("code", 300);
            outDto.put("change_list", change);
            return ResponseData.success(outDto);
        } else if (count_2 > 0) {
            outDto.put("code", 400);
            return ResponseData.success(outDto);
        } else {//修改分销订单及ERP订单数据
            for (GwdfT gwdfT : gwdfTList) {
                if (gwdfT.getGWDF004() == null || ("").equals(gwdfT.getGWDF004())) {
                    //查询ERP数据，获取该单号最大项次
                    Dto maxTD003 = this.gwdfTService.getMaxTD003(gwdfT.getGWDFMLEGAL(), gwdfT.getGWDF003());
                    Dto gwamQ = Dtos.newDto();
                    gwamQ.put("ENT", inDto.getString("ENT"));
                    gwamQ.put("LANG", inDto.getString("LANG"));
                    gwamQ.put("GWAM001", gwdfT.getGWDF011());
                    Dto gwam = this.gwdfTService.getGwam(gwamQ);
                    GwcsT gwcsTQ = this.gwdfTService.getMaxGwcsseq(gwdfT.getGWDF001());
                    try {
                        this.gwdfTService.addGwcs(gwcsTQ, gwdfT);
                        this.gwdfTService.insertCOPTD(maxTD003, gwdfT, gwam, gwcsTQ.getGWCSSEQ().add(new BigDecimal(1)));
                    } catch (Exception e) {
                        BigDecimal TD003 = maxTD003.getBigDecimal("TD003_MAX").add(new BigDecimal(1));
                        DecimalFormat df = new DecimalFormat("0000");
                        String td003 = df.format(TD003);
                        Dto delCoptc = Dtos.newDto();
                        delCoptc.put("mlegal", gwdfT.getGWDFMLEGAL());
                        delCoptc.put("TD002", gwdfT.getGWDF003());//订单号
                        delCoptc.put("TD003", td003);//订单项次
                        this.gwdfTService.delGwcs(gwcsTQ);
                        this.gwdfTService.delCOPTD(delCoptc);
                        e.printStackTrace();
                        outDto.put("code", 500);
                        outDto.put("message", "订单变更单确认失败！" + e.getMessage());
                        return ResponseData.success(outDto);
                    }
                }
                //修改ERP数据
                Dto COPTD = Dtos.newDto();
                COPTD.put("mlegal", gwdfT.getGWDFMLEGAL());
                COPTD.put("TC038", gwdfT.getGWDF031());//备注
                COPTD.put("UDF12", gwdfT.getGWDF007());//返利类型
                switch (gwdfT.getGWDF007()) {
                    case "1":
                        COPTD.put("UDF12", "现款有返利");
                        break;
                    case "2":
                        COPTD.put("UDF12", "现款无返利");
                        break;
                    case "3":
                        COPTD.put("UDF12", "帐期有返利");
                        break;
                    case "4":
                        COPTD.put("UDF12", "帐期无返利");
                        break;
                    case "5":
                        COPTD.put("UDF12", "铺货有返利");
                        break;
                    case "6":
                        COPTD.put("UDF12", "铺货无返利");
                        break;
                }
                COPTD.put("TD008", gwdfT.getGWDF014());//订单数量
                COPTD.put("TD007", gwdfT.getGWDF023());//仓库
                //COPTD.put("TD011", gwdfT.getGWDF017());//价格
                COPTD.put("TD012", gwdfT.getGWDF026());//金额
                COPTD.put("TD039", gwdfT.getGWDF029());//税额
                COPTD.put("TD038", gwdfT.getGWDF026().subtract(gwdfT.getGWDF029()));//税前金额
                COPTD.put("TDI01", COPTD.getBigDecimal("TD038").multiply(gwdfT.getGWDF024()));//本币税前金额
                COPTD.put("TDI02", gwdfT.getGWDF029().multiply(gwdfT.getGWDF024()));//本币税前金额
                COPTD.put("TDI03", gwdfT.getGWDF026().multiply(gwdfT.getGWDF024()));//本币金额
                COPTD.put("TD002", gwdfT.getGWDF003());//订单号
                COPTD.put("TD003", gwdfT.getGWDF004());//订单项次
                COPTD.put("TD026", gwdfT.getGWDF036());//折扣率

                //更新分销订单数据
                GwcsT gwcsT = new GwcsT();
                //gwcsT.setGWCS050(gwdfT.getGWDF031());//备注
                //gwcsT.setGWCS117(gwdfT.getGWDF007());//返利类型
                gwcsT.setGWCS003(gwdfT.getGWDF014());//订单数量
                gwcsT.setGWCS113(gwdfT.getGWDF023());//仓库
                gwcsT.setGWCS004(gwdfT.getGWDF017());//价格
                gwcsT.setGWCS005(gwdfT.getGWDF026());//金额
                gwcsT.setGWCS040(gwdfT.getGWDF029());//税额
                gwcsT.setGWCS112(gwdfT.getGWDF021());//仓库类型
                gwcsT.setGWCS035(gwdfT.getGWDF036());//折扣
                gwcsT.setGWCSDOCNO(gwdfT.getGWDF001());//订单号
                gwcsT.setGWCSSEQ(gwdfT.getGWDF005());//订单项次
                gwcsT.setGWCSENT(gwdfT.getGWDFENT());

                GwcsT gwcsTNew = new GwcsT();
                gwcsTNew.setGWCS117(gwdfT.getGWDF007());//返利类型
                gwcsTNew.setGWCSDOCNO(gwdfT.getGWDF001());//订单号
                gwcsTNew.setGWCS050(gwdfT.getGWDF031());//备注
                gwcsTNew.setGWCSENT(gwdfT.getGWDFENT());

                //更新赠品
                GwcszT gwcszT = new GwcszT();
                gwcszT.setGWCSZENT(gwdfT.getGWDFENT());
                gwcszT.setGWCSZDOCNO(gwdfT.getGWDF001());
                gwcszT.setGWCSZSEQ(gwdfT.getGWDF004());//erp变更单项序
                gwcszT.setGWCSZ005(gwdfT.getGWDF014());//价格
                try {
                    this.gwdfTService.updateGWCS_GWCSZ(gwcsT, gwcszT, gwcsTNew);
                    this.gwdfTService.updateCOPTC_D(COPTD);
                } catch (Exception e) {
                    e.printStackTrace();
                    outDto.put("code", 500);
                    outDto.put("message", "订单变更单确认失败！" + e.getMessage());
                    return ResponseData.success(outDto);
                }

            }
            this.gwdfTService.updateGwdf(inDto);
        }*/

    }

    /**
     * 修改销货数量
     */
    @RequestMapping("/updateGwdf015")
    @RequiresPermissions("crm:gwdc005:init")
    @ResponseBody
    public ResponseData updateGwdf015(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
        List<GwdfT> gwdfTList = JSONObject.parseArray(jsonObject.getString("list"), GwdfT.class);
        gwdfTService.updateGwdf015(gwdfTList, jsonObject.getString("GWDFDOCNO"));
        return SUCCESS_TIP;
    }

    /**
     * 获取订单开窗数据
     */
    @RequestMapping("/getGwcsInfo")
    @ResponseBody
    public ResponseData getGwcsInfo(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> gwcsInfo = gwdfTService.getGwcsList(inDto);
        return new SuccessResponseData(gwcsInfo);
    }

    /**
     * 获取仓库上调比例
     */
    @RequestMapping("/getGwak063")
    @RequiresPermissions("crm:gwai770:init")
    @ResponseBody
    public ResponseData getGwak063(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto gwak063 = gwdfTService.getGwak063(inDto);
        return new SuccessResponseData(gwak063);
    }

    /**
     * 查询结算价
     */
    @RequestMapping("/getPrice")
    @ResponseBody
    public ResponseData getPrice(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwdcT> select_data = inDto.getReqList(GwdcT.class);
        List<Dto> list = new ArrayList<>();
        for (GwdcT gwdcT : select_data) {
            inDto.put("GWDC007", gwdcT.getGWDC007());//对象类型1：渠道 2：渠道商
            inDto.put("GWDC016", gwdcT.getGWDC016().trim());//客户编号
            inDto.put("GWDC018", gwdcT.getGWDC018());//产品编号
            inDto.put("GWDC008", gwdcT.getGWDC008());//单据日期
            inDto.put("GWAK001", gwdcT.getGWDC016().trim());//客户
            Dto dto = gwdc003Service.getGwdc023(inDto);
            Dto qdto = gwdfTService.getGwak063(inDto);
            if (qdto != null && dto != null) {
                dto.put("GWAK058", qdto.getString("GWAK058"));
                dto.put("GWAK063", qdto.getString("GWAK063"));
                dto.put("GWAS003", qdto.getString("GWAS003"));
            }
            if (dto != null) {
                list.add(dto);
            }
        }
        return ResponseData.success(list);
    }
}
